package com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.normal;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.fgs.common.CommonResponse;
import com.fgs.common.manager.ActivityManager;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.GoodsInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.DeliverGoodsSuccessActivity;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2020/9/1
 * @description
 **/
public abstract class DeliverGoodsActivity extends BaseActivity {

    @BindView(R.id.layout_deliver_status_firstArrowImageView)
    ImageView mFirstArrowImageView;
    @BindView(R.id.layout_deliver_status_secondArrowImageView)
    ImageView mSecondArrowImageView;
    @BindView(R.id.layout_deliver_status_thirdArrowImageView)
    ImageView mThirdArrowImageView;

    @BindView(R.id.layout_deliver_status_secondImageView)
    ImageView mSecondImageView;
    @BindView(R.id.layout_deliver_status_thirdImageView)
    ImageView mThirdImageView;
    @BindView(R.id.layout_deliver_status_fourthImageView)
    ImageView mFourthImageView;

    @BindView(R.id.layout_deliver_status_secondTextView)
    TextView mSecondTextView;
    @BindView(R.id.layout_deliver_status_thirdTextView)
    TextView mThirdTextView;
    @BindView(R.id.layout_deliver_status_fourthTextView)
    TextView mFourthTextView;

    protected GoodsInfo mGoodsInfo;
    protected OrderModel mOrderModel;

    @Override
    public void init() {
        mOrderModel = new OrderModel();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String goodsId = bundle.getString(KeyValue.KEY_ID);
            if (!TextUtils.isEmpty(goodsId)) {
                requestGoodsDetail(goodsId);
            } else {
                mGoodsInfo = bundle.getParcelable(KeyValue.KEY_GOODS_INFO);
                LogUtil.print(JsonUtil.toJson(mGoodsInfo));
            }

        }
        if (mGoodsInfo == null) {
            mGoodsInfo = new GoodsInfo();
        }

        initData();
        initView();
        initListener();
    }

    public abstract void initData();

    public abstract void initView();

    public abstract void initListener();

    private void requestGoodsDetail(String goodsId) {
        Map<String, String> map = new HashMap<>();
        map.put("id", goodsId);
        mOrderModel.orderDetailModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    GoodsInfo goodsInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), GoodsInfo.class);
                    if (goodsInfo != null) {
                        mGoodsInfo = goodsInfo;
                        initData();
                        initView();
                    } else {
                        showToast("未找到货源信息");
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    protected void showStepTwoStatus() {
        mFirstArrowImageView.setImageResource(R.mipmap.icon_deliver_arrow_selected);
        mSecondImageView.setImageResource(R.mipmap.icon_deliver_source_select_status);
        mSecondTextView.setSelected(true);
    }

    protected void showStepThreeStatus() {
        showStepTwoStatus();
        mSecondArrowImageView.setImageResource(R.mipmap.icon_deliver_arrow_selected);
        mThirdImageView.setImageResource(R.mipmap.icon_deliver_order_selected_status);
        mThirdTextView.setSelected(true);
    }

    protected void showStepFourStatus() {
        showStepThreeStatus();
        mThirdArrowImageView.setImageResource(R.mipmap.icon_deliver_arrow_selected);
        mFourthImageView.setImageResource(R.mipmap.icon_deliver_other_selected_status);
        mFourthTextView.setSelected(true);
    }

    @Override
    public String getToolbarTitle() {
        return "发布货源";
    }

    protected void openNextStepActivity(Class pClass) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KeyValue.KEY_GOODS_INFO, mGoodsInfo);
        openActivity(pClass, bundle);
    }

    protected void requestPublishSource() {
        mOrderModel.publishSourceModel(this, mGoodsInfo, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        openActivity(DeliverGoodsSuccessActivity.class);
                        ActivityManager.getInstance().finishActivity(DeliverGoodsStepOneActivity.class);
                        ActivityManager.getInstance().finishActivity(DeliverGoodsStepTwoActivity.class);
                        ActivityManager.getInstance().finishActivity(DeliverGoodsStepThreeActivity.class);
                        finish();
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }
}
